package com.example.blogsystemssm.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 被拦截器拦下的，会进入实现的拦截功能
 * 比如被registration拦截了除exclude之外的链接，会进入被传入的拦截器loginInterceptor中
 * loginInterceptor中实现了preHandle功能，就会在正式进入原本指向的方法之前先执行preHandle中的方法
 */
@Configuration
public class AppConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;

    List<String> excludes = new ArrayList<String>() {{
//        "/**" 表示所有目录下或者所有文件
        add("/**/*.html");
        add("/**/*.js");
        add("/css/**");
        add("/img/**");
        add("/editor.md/**");
        add("/user/login");
        add("/user/reg");
        add("/art/detail");
    }};

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(loginInterceptor);
        registration.addPathPatterns("/**");
        registration.excludePathPatterns(excludes);
    }
}
